package com.project.utils;

import com.project.bean.DetailBean;
import com.project.bean.OrderBean;
import com.project.dto.merchantToRider.DetailDto;
import com.project.dto.merchantToRider.OrderDto;
import com.project.mapper.DetailMapper;
import com.project.mapper.OrderMapper;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @author lyl
 * description:
 */
@Service
public class RabbitMqUtil {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private DetailMapper detailMapper;

    /**
     * 从队列中获取相应的订单dto对象并将其实例化至数据库中
     *
     * @param orderDto
     * @throws IOException
     */
    @RabbitListener(queues = "MerchantQueue")
    public void getOrder(OrderDto orderDto) throws IOException {
        if (orderDto != null) {
            System.out.println(orderDto);
            OrderBean ob = new OrderBean();
            //起始时间
            ob.setStartTime(orderDto.getStartTime());
            //设置订单code
            ob.setCode(orderDto.getCode());
            //状态
            ob.setStatue(orderDto.getStatue());
            //区域
            ob.setArea(orderDto.getArea());
            //总价
            ob.setPrice((int) Double.parseDouble(orderDto.getPrice()));
            //商家联系电话
            ob.setMerchantTel(orderDto.getMerchantTel());
            //商家地址
            ob.setMerchantAddress(orderDto.getMerchantAddress());
            //客户联系电话
            ob.setCustomerTel(orderDto.getCustomerTel());
            //客户地址
            ob.setCustomerAddress(orderDto.getCustomerAddress());
            //订单备注
            ob.setOrderNotes(orderDto.getOrderNotes());


            //将订单实例化至数据库中
            orderMapper.insert(ob);
            //获取订单详情信息
            List<DetailDto> listDto = orderDto.getList();
            for (DetailDto detailDto : listDto) {
                DetailBean detailBean = new DetailBean();
                detailBean.setOrderId(orderDto.getCode());
                detailBean.setProductName(detailDto.getProductName());
                detailBean.setProductPrice(detailDto.getProductPrice());
                detailBean.setProductNum(detailDto.getProductNum());
                detailBean.setProductSumPrice(detailDto.getProductSumPrice());
                //将订单详情实例化至数据库中
                detailMapper.insert(detailBean);
            }
        }
    }
}
